/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.*;
import Model.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import library.Koneksi;


/**
 *
 * @author mr2
 */
public class BeritaTALogic{
    HttpServletRequest request;

    public BeritaTALogic(HttpServletRequest request) {
        this.request = request;
    }
    
    //Logic ini digunakan untuk menampilkan halaman kelola berita TA pada akun koordinator TA
    public String KelolaBeritaTA(){
        
        String pesan = request.getParameter("pesan");
        String judulBerita = request.getParameter("judulBerita");
        
        HttpSession session = request.getSession();
        ArrayList listRole = new ArrayList();
        listRole = (ArrayList)session.getAttribute("role");
        
        boolean bukanRole = true;
        if (session.getAttribute("role") != null) {
            for (int i = 0; i < listRole.size(); i++) {
                if ((listRole.get(i).equals("koordinator_ta"))) {
                    bukanRole = false;
                }
            }
        }
        
        if (session.getAttribute("role") == null) {
            return "login";
        } else if (bukanRole) {
            return "login";
        }else {
            String idUser = (String) session.getAttribute("id_user");
            BeritaTAModel beritaTA = new BeritaTAModel();
            List<BeritaTA> listBeritaTA = beritaTA.ListBeritaTA();
            
            request.setAttribute("idUser", idUser);
            request.setAttribute("listBeritaTA", listBeritaTA);
            request.setAttribute("pesan", pesan);
            request.setAttribute("judulBerita", judulBerita);

            request.setAttribute("menu", "monitoringTA");
            request.setAttribute("submenu", "kelolaBeritaTA");

            return "monta/KoorTA_kelola_beritaTA.jsp";
        }
    }
    
    //Logic menampilkan form pembuatan berita TA
    public String formBeritaTA(){
        
        HttpSession session = request.getSession();
        ArrayList listRole = new ArrayList();
        listRole = (ArrayList)session.getAttribute("role");
        
        boolean bukanRole = true;
        if (session.getAttribute("role") != null) {
            for (int i = 0; i < listRole.size(); i++) {
                if ((listRole.get(i).equals("koordinator_ta"))) {
                    bukanRole = false;
                }
            }
        }
        
        if (session.getAttribute("role") == null) {
            return "login";
        } else if (bukanRole) {
            return "login";
        }else{
            String idUser = request.getParameter("idUser");
            request.setAttribute("idUser", idUser);

            request.setAttribute("menu", "monitoringTA");
            request.setAttribute("submenu", "kelolaBeritaTA");

            return "monta/KoorTA_form_beritaTA.jsp";
        } 
    }
    
    //Logic untuk memanggil model untuk menyimpan berita TA yang telah ditulis pada form berita TA
    public String BuatBeritaTA(){
               
        String idUser = request.getParameter("idUser");
        String judulBerita = request.getParameter("judulBerita");
        String isiBerita = request.getParameter("isiBerita");
        String pesan = null;
        
        if(idUser != null){
            
            User user = new User();
            BeritaTA beritaTA = new BeritaTA();
            BeritaTAModel berita = new BeritaTAModel();
            
            user.setIdUser(idUser);
            beritaTA.setUser(user);
            beritaTA.setJudulBerita(judulBerita);
            beritaTA.setIsiBerita(isiBerita);
                        
            boolean BuatBeritaTA = berita.BuatBeritaTA(beritaTA);
            
            if(BuatBeritaTA){
                pesan = "sukses buat berita";
            }else{
                pesan = "gagal buat berita";
            }
            System.out.println(pesan);
            
            return "SidangTAController?page=kelolaBeritaTA&&pesan="+pesan+"&&judulBerita="+judulBerita;
            
        }else{
            return KelolaBeritaTA();
        }
    }
    
    //Logic untuk menampilkan detail dari isi berita TA
    public String DetailBeritaTA(){
        
        String idBerita = request.getParameter("idBerita");
        int idberita = Integer.parseInt(idBerita);
        
        BeritaTA beritaTA = new BeritaTA();
        BeritaTAModel berita = new BeritaTAModel();
        
        beritaTA.setIdBerita(idberita);
        
        BeritaTA detailBerita = berita.DetailBerita(beritaTA);
        request.setAttribute("detailBerita", detailBerita);
        request.setAttribute("menu", "monitoringTA");
        request.setAttribute("submenu", "kelolaBeritaTA");
        
        return "monta/KoorTA_detail_beritaTA.jsp";
    }
    
    //Logic untuk memanggil method hapusBerita pada file BeritaTAModel.java untuk menghapus berita TA
    public String HapusBeritaTA(){
        
        String idBerita = request.getParameter("idBerita");
        String judulBerita = request.getParameter("judulBerita");
        int idberita = Integer.parseInt(idBerita);
        String pesan = null;
        
        BeritaTA beritaTA = new BeritaTA();
        BeritaTAModel berita = new BeritaTAModel();
        
        beritaTA.setIdBerita(idberita);
        
        boolean hapusBerita = berita.hapusBerita(beritaTA);
        
        if(hapusBerita){
            pesan = "sukses hapus berita";
        }else{
            pesan = "gagal hapus berita";
        }
       
        
        return "SidangTAController?page=kelolaBeritaTA&&pesan="+pesan+"&&judulBerita="+judulBerita;
    }
    
    //Logic untuk memangggil method
    public String ListBeritaTA(){
        
        BeritaTAModel beritaTA = new BeritaTAModel();
        List<BeritaTA> listBeritaTA = beritaTA.ListBeritaTA();
        
        UserModel user = new UserModel();
        List<User> listUser = user.listUser();
        
        request.setAttribute("listUser", listUser);
        request.setAttribute("listBeritaTA", listBeritaTA);
        request.setAttribute("menu", "monitoringTA");
        request.setAttribute("submenu", "beritaTA");
        
        return "monta/ListBeritaTA.jsp";
    }
    
    public String BeritaTAFull(){
        
        String idBerita = request.getParameter("idBerita");
        int idberita = Integer.parseInt(idBerita);
        
        BeritaTA beritaTA = new BeritaTA();
        BeritaTAModel berita = new BeritaTAModel();
        
        beritaTA.setIdBerita(idberita);
        
        BeritaTA detailBerita = berita.DetailBerita(beritaTA);
        request.setAttribute("detailBerita", detailBerita);
        
        request.setAttribute("menu", "monitoringTA");
        request.setAttribute("submenu", "beritaTA");
        
        return "monta/BeritaTAFull.jsp";
    }
    
    
        
}
